package com.rocky.project.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rocky.project.service.BaseService;

/**
 * 
 */

/**
 * 用来导入导出基础数据的入口
 * @author chen
 * 
 */
public class ImporterMain {

	private static BaseService baseService;

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] { "spring.xml" });
		BeanFactory factory = (BeanFactory) appContext;
		baseService = (BaseService) factory.getBean("baseService");
		System.out.println("请输入要执行的导入操作：1-》导入农户信息；2-》导入图片信息；");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String opt = br.readLine();
		if ("1".equals(opt)) {
			family();
		} else if ("2".equals(opt)) {
			archive();
		} else {
			System.out.println("您输入的不是有效操作，退出！");
		}
		System.out.println("导入退出！");
		System.exit(0);
	}





	public static void archive() throws IOException {
		//E:\\project\\杨家角
		ArchiveImporterImpl importer = new ArchiveImporterImpl();
		importer.setBaseService(baseService);
		System.out.println("请输入要导入的文件夹的路径：");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while(true){
			String path = br.readLine();		
			if(StringUtils.isNotBlank(path)){
				importer.setPath(path);		
				importer.beginImport();
				break;
			}
		}
	}



	public static void family() throws IOException {
		PersonImporterImpl importer = new PersonImporterImpl();
		importer.setBaseService(baseService);
		System.out.println("请输入要导入的文件夹的路径：");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while(true){
			String path = br.readLine();		
			if(StringUtils.isNotBlank(path)){
				importer.setPath(path);		
				importer.beginImport();
				break;
			}
		}
		

	}

}
